//
//  FollowViewController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/10/27.
//

#import "FollowViewController.h"
#import "FollowTableView.h"
#import "SearchView.h"
#import "ShopViewController.h"
#import "FollowTipsView.h"
#import "ScanCodeViewController.h"

@interface FollowViewController ()

@property (nonatomic, strong) FollowTableView *tableview;
@property (nonatomic, strong) SearchView *searchV;
@property (nonatomic, copy) NSString *searchStr;
@property (nonatomic, strong) NSMutableArray *modelArr;

@property (nonatomic, assign) NSInteger searchPage;

@end

@implementation FollowViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)initDidLoad {
    self.hbd_barAlpha = 0;
}
- (void)initViews {
    [self.view addSubview:self.searchV];
    [self.view addSubview:self.tableview];
    
    [self.searchV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(0);
        make.top.mas_equalTo(kHeightForStatusBar);
        make.height.mas_equalTo(56);
    }];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.mas_equalTo(0);
        make.top.mas_equalTo(self.searchV.mas_bottom);
    }];
}

- (void)requestData {
    [YPProgressHud showHudInview:self.view];
    [NET getFollowList:@(self.page) size:@(self.pageSize) success:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        NSMutableArray *arr = [UserModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]];
        if (self.page == 1) {
            [self.modelArr removeAllObjects];
        }
        [self.modelArr addObjectsFromArray:arr];
        self.tableview.modelArr = self.modelArr;
        [self.tableview reloadData];
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        kShowText(error.userInfo[kHttpErrorReason]);
        [YPProgressHud hideHudForView:self.view];
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
    }];
}

- (void)showFollowStatus:(UserModel *)model {
    if (!model.follow) {
        FollowTipsView *v = [[FollowTipsView alloc] init];
        [v show];
        @weakify(self)
        [v setFollowBlock:^(NSInteger type) {
         @strongify(self)
            [self followUnfollow:model status:type];
        }];
    }else {
        [self followUnfollow:model status:1];
    }
}
- (void)followUnfollow:(UserModel *)model status:(NSInteger)status {
    [YPProgressHud showHudInview:self.view];
    [NET followUser:model.id flag:!model.follow status:status success:^(id  _Nonnull responseObject) {
        model.follow = !model.follow;
        [self.modelArr enumerateObjectsUsingBlock:^(UserModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.id isEqualToString:model.id]) {
                [self.modelArr replaceObjectAtIndex:idx withObject:model];
            }
        }];
        self.tableview.modelArr = self.modelArr;
        [self.tableview reloadData];
        [self removeFollowStatsView];
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

- (void)topUser:(UserModel *)model {
    [YPProgressHud showHudInview:self.view];
    [NET topUser:model.id type:!model.top success:^(id  _Nonnull responseObject) {
        self.page = 1;
        [self requestData];
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        kShowText(error.userInfo[kHttpErrorReason]);
    }];
}

- (void)searchUser {
    [YPProgressHud showHudInview:self.view];
    [NET searchUser:self.searchStr pageNumber:@(self.searchPage) pageSize:@(20) success:^(id  _Nonnull responseObject) {
        NSMutableArray *arr = [UserModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]];
        if (self.searchPage == 1) {
            [self.modelArr removeAllObjects];
        }
        [self.modelArr addObjectsFromArray:arr];
        self.tableview.modelArr = self.modelArr;
        [self.tableview reloadData];
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        [YPProgressHud hideHudForView:self.view];
    } failure:^(NSError * _Nonnull error) {
        kShowText(error.userInfo[kHttpErrorReason]);
        [YPProgressHud hideHudForView:self.view];
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
    }];
}

#pragma mark - 懒加载
- (FollowTableView *)tableview {
    if (!_tableview) {
        _tableview = [[FollowTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        @weakify(self)
        _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         @strongify(self)
            if (self.searchStr.length > 0) {
                self.searchPage = 1;
                [self searchUser];
            }else {
                self.page = 1;
                [self requestData];
            }
        }];
        _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self)
            if (self.searchStr.length >0) {
                self.searchPage++;
                [self searchUser];
            }else {
                self.page++;
                [self requestData];
            }
        }];
        [_tableview setFollowBtnBlock:^(UserModel * _Nonnull model) {
            @strongify(self)
            [self showFollowStatus:model];
        }];
        [_tableview setTopBtnBlock:^(UserModel * _Nonnull model) {
            @strongify(self)
            [self topUser:model];
        }];
        [_tableview.subject subscribeNext:^(NSIndexPath *x) {
            @strongify(self)
            UserModel *model = self.modelArr[x.row];
            ShopViewController *vc = [[ShopViewController alloc] init];
            vc.userId = model.id;
            vc.isPush = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _tableview;
}

- (SearchView *)searchV {
    if (!_searchV) {
        _searchV = [[SearchView alloc] init];
        _searchV.type = 1;
        @weakify(self)
        _searchV.searchBlock = ^(NSString * _Nonnull searchStr) {
         @strongify(self)
            self.searchStr = searchStr;
            self.searchPage = 1;
            [self searchUser];
        };
        _searchV.clearBlcok = ^{
          @strongify(self)
            self.searchStr = nil;
            self.page = 1;
            self.searchPage = 1;
            [self requestData];
        };
        _searchV.qrBlock = ^{
          @strongify(self)
            ScanCodeViewController *vc = [[ScanCodeViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
    }
    return _searchV;
}

- (NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}

@end
